home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / patchlib539.lha / patchlib / examples / longdelay / LongDelay.c < prev    next >
C/C++ Source or Header  |  1996-10-17  |  3KB  |  107 lines

  1. /* This is an example, how to implement library patching in pure SAS/C */
  2. /* with patch.library V5+ */
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <dos.h>
  8. #include <exec/types.h>
  9. #include <exec/libraries.h>
  10. #include <utility/tagitem.h>
  11. #include <clib/exec_protos.h>
  12. #include <pragmas/exec_pragmas.h>
  13. #include "ASMSources:PatchLibrary/PatchInclude/patch_protos.h"
  14. #include "ASMSources:PatchLibrary/PatchInclude/patch_pragmas.h"
  15. #include "ASMSources:PatchLibrary/PatchInclude/patchtags.h"
  16. #include "ASMSources:PatchLibrary/PatchInclude/patch.h"
  17.  
  18. #define    ASMFLAGS __saveds __asm
  19. #define REGD0 register __d0
  20. #define REGD1 register __d1
  21. #define REGD2 register __d2
  22. /* ... */
  23. #define REGA0 register __a0
  24. #define REGA1 register __a1
  25. #define REGA2 register __a2
  26. /* ... */
  27. #define REGA6 register __a6
  28.  
  29. #define LVO_Delay        -198
  30.  
  31. char *ver="$VER: LongDelay 1.00 (17.10.96)  by Stefan Fuchs";
  32. extern struct ExecBase *SysBase;
  33. struct PatchBase *PatchBase = 0;
  34. struct Patch *PatchPtr;
  35.  
  36. void Open_All(void);
  37. void Close_All(STRPTR);
  38. ASMFLAGS struct PatchXResult *LongDelay(REGD1 ULONG, REGA6 struct Library *);
  39.  
  40. void main()
  41. {
  42. ULONG sigrcvd;
  43. BOOL weiter = TRUE;
  44.  
  45.     printf("LongDelay V1.0  © 1996 by Stefan Fuchs\n");
  46.     Open_All();
  47.     printf("Library patches successfully installed!\n");
  48.     printf("Press CTRL-c to quit\n");
  49.  
  50.     while(weiter)
  51.     {
  52.         sigrcvd = Wait(SIGBREAKF_CTRL_C);
  53.         if (sigrcvd & SIGBREAKF_CTRL_C) weiter = FALSE;
  54.     }
  55.  
  56.     Close_All("");
  57. }
  58.  
  59. void Open_All()
  60. {
  61.     if((PatchBase = (struct PatchBase *) OpenLibrary("patch.library", 5L)) == NULL)
  62.         Close_All("Can't open V5+ patch.library !\n");
  63.     PatchPtr = InstallPatchTags( (ULONG (*)())LongDelay,
  64.             LVO_Delay, 
  65.             PATT_LibraryName,"dos.library",
  66.             PATT_PatchName,"LongDelay",
  67.             PATT_Priority,10,
  68.             PATT_StackSize,1000L,
  69.             PATT_UseXResult,TRUE,
  70.             TAG_DONE);
  71. }
  72.  
  73. void Close_All(s)
  74. STRPTR s;
  75. {
  76.     printf("%s",s);
  77.     if(PatchPtr) RemovePatchTags(PatchPtr, PATT_TimeOut, 800L, TAG_DONE);
  78.     if(PatchBase) CloseLibrary((struct Library *)PatchBase);
  79.     exit(TRUE);
  80. }
  81.  
  82.  
  83. /* The following patch does almost nothing, it simply increases the number of ticks */
  84. /* until Delay() returns */
  85.  
  86. /* As C-Functions may destroy the registers d0/d1/a0/a1 you must return them in */
  87. /* the extended result structure */
  88.  
  89. ASMFLAGS struct PatchXResult *LongDelay(REGD1 ULONG ticks, REGA6 struct Library *DosBase)
  90. {
  91. struct PatchXResult *xresult;
  92.  
  93.     ticks += 30;            /* Do your stuff here */
  94.  
  95.     /* Allocate an extended result structure */
  96.     /* patch.library will free this, when the patch is completed */
  97.     /* This function better succeeds, as setting registers wrong */
  98.     /* will in most cases cause crashes */
  99.  
  100.     if (xresult = (struct PatchXResult *)PatchAlloc(PS_TYPE_XRESULT))
  101.     {
  102.         xresult->pxr_RegPattern = PATREG_D1;    /* 'Or' all registers, you change here */
  103.         xresult->pxr_RegD1 = ticks;        /* fill these registers in the appropriate fields */
  104.     }
  105. return (xresult);                    /* return the extended return structure */
  106. }
  107.